<!-- Horizontal Form -->
<div class="box box-info">
    <div class="box-header with-border">
        <h3 class="box-title">商品添加</h3>
        <h3 class="msg"></h3>
    </div>
    <!-- /.box-header -->
    <!-- form start -->
    <form class="form-horizontal">
        <div class="box-body">
            <div class="form-group">
                <label for="actualPriceId" class="col-sm-2 control-label">折后价格</label>
                <div class="col-sm-8">
                    <input type="text" name="actualPrice" class="form-control" id="actualPriceId">
                </div>
            </div>
            <div class="form-group">
                <label for="numId" class="col-sm-2 control-label">购买数量</label>
                <div class="col-sm-8">
                    <input type="text" name="num" class="form-control" id="numId">
                </div>
            </div>
        </div>
        <!-- /.box-body -->
        <div class="box-footer">
            <button type="button" class="btn btn-default btn-cancel">退出</button>
            <button type="button" class="btn btn-info pull-right btn-save">保存</button>
        </div>
        <!-- /.box-footer -->
    </form>

</div>
<script type="text/javascript" src="bower_components/ztree/jquery.ztree.all.min.js"></script>
<script type="text/javascript" src="bower_components/layer/layer.js"></script>
<script type="text/javascript">

    $(document).ready(function () {
        //页面加载完成以后加载角色信息并初始化页面
        doInitFormData();
        //事件注册
        $(".box-footer")
            .on("click", ".btn-cancel", doCancel)   //触发取消
            .on("click", ".btn-save", doSaveOrUpdate);  //触发新增/修改
    });

    //取消
    function doCancel() {
        $("#mainContentId").removeData("rowData");
        $("#mainContentId").load("/modules/sys/order_sub_list.html");
    }

    //新增/修改
    function doSaveOrUpdate() {
        let rowData = $("#mainContentId").data("rowData"); //读取修改的数据
        //1.获取表单数据
        let data = doGetEditFormData();
        data.id = rowData.id; //如果有数据就是进行修改,id等于选中的id
        if(data.actualPrice==0 || data.num ==0){
            alert("不能为0!");return;
        }
        //2.发起异步请求
        let url = `${baseUrl}/shoppingback/ddSubOrderInfo`;
        $.ajax({
            method: "put",
            url: url,
            headers : {"Authorization": "Bearer " + window.localStorage.getItem("accessToken")},
            crossDomain: true,
            xhrFields: {withCredentials: true},
            data: JSON.stringify(data),
            contentType: "application/json",
            success(result) {
                if(result.status==401){
                    alert(result.msg);
                    return;
                }
                if(result.status==403){
                    alert(result.msg);
                    return;
                }
                if (result.success) {
                    alert(result.msg);
                    doCancel();
                } else {
                    alert(result.msg);
                }
            }
        })
    }

    //添加用户:读取输入的数据
    function doGetEditFormData() {
        //获取用户输入的数据
        let params = {
            actualPrice: $("#actualPriceId").val(),
            num: $("#numId").val()
        }
        return params;
    }

    //修改用户:读取表单数据
    function doInitFormData() {
        let data = $("#mainContentId").data("rowData");
        if (!data) return; //如果不是修改则返回
        //初始化用户信息
        $("#actualPriceId").val(data.actualPrice);
        $("#numId").val(data.num);
    }

</script>
    
    
    
    
    
    
    